package com.example.redistest.ThreadDemo;

import javax.sound.midi.Soundbank;

public class ThreadDemo4 implements Runnable {
    private String name;
    public ThreadDemo4(String name){
        this.name = name;
    }
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName()+"线程正在运行");
        for (int i=0; i<10; i++){
            System.out.println(name+"运行"+i);
        }
        try {

            Thread.sleep((int) Math.random() * 10);
            System.out.println(Thread.currentThread().getName()+"线程结束运行");
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        System.out.println("主线程正在运行");
        Thread thread1 = new Thread(new ThreadDemo4("A"));
        Thread thread2 = new Thread(new ThreadDemo4("B"));
        thread2.start();
        thread1.start();
        try {
            thread1.join();
        }catch (Exception e){
            e.printStackTrace();
        }
        try {
            thread2.join();
        }catch (Exception e){
            e.printStackTrace();
        }
        System.out.println("主线程结束运行");
    }
}
